package com.xgame.server.dbtool;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.xgame.server.db.mapper.GamerecordMapper;
import com.xgame.server.db.model.Gamerecord;
import com.xgame.server.netty.proto.Game;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class GamerecordCsTool {
    @Resource
    private GamerecordMapper gamerecordMapper;

    //插入经验副本
    public Integer insertGameRecord(Game.GameRecordCS data) throws Exception {
        //1.删除本玩家的所有
        deleteGameRecord(data);

        //2.插入
        List<Game.GameRecordOneCS> items = data.getListList();
        int length = items.size();
        Integer count = new Integer(0);
        for (int i = 0; i < length; i++) {
            Game.GameRecordOneCS one = items.get(i);
            Gamerecord map = new Gamerecord();
            map.setAccount(data.getAccount());
            map.setPlayerId(data.getPlayerID());
            map.setOp(new Long(one.getOp()));
            map.setCoin(one.getCoin());
            map.setDiamond(one.getDiamond());
            map.setMsg(one.getMsg());
            map.setTime(one.getTime());
            map.setDate(one.getDate());

            count += gamerecordMapper.insert(map);
        }

        return count;
    }

    //删除记录
    public Integer deleteGameRecord(Game.GameRecordCS data) throws Exception {
        EntityWrapper<Gamerecord> userWrapper = new EntityWrapper<>();
        userWrapper.eq("account", data.getAccount());

        return gamerecordMapper.delete(userWrapper);
    }

    //查询记录
    public List<Gamerecord> selectGameRecord(String account) throws Exception {
        EntityWrapper<Gamerecord> userWrapper = new EntityWrapper<>();
        userWrapper.eq("account", account);

        return gamerecordMapper.selectList(userWrapper);
    }
}
